package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Qwa;
import com.example.demo.mapper.QwaMapper;
import com.example.demo.service.IQwaService;
import com.example.demo.service.PersonService;
import com.example.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author king
 * @since 2024-05-23
 */

@RestController
@Api(tags = "问答对管理")
@CrossOrigin
@RequestMapping("/qwa")
public class QwaController {
    @Autowired
    private IQwaService qwaService;
    @Autowired
    private QwaMapper qwaMapper;
    @Autowired
    private PersonService personService;
    @ApiOperation("获取当前问答对信息")
    @GetMapping("/getQwa")
    public IPage<Qwa> getQwa(@RequestParam Integer pageNo
            , @RequestParam Integer pageSize
//            , @RequestParam(defaultValue = "") String  userName
//            , @RequestParam(defaultValue = "") String  date
//            , @RequestParam(defaultValue = "") String  num
    ) {

//        方法1：
//        pageNo=(pageNo - 1)*pageSize;
////        System.out.println(pageNo);
//        Map<String ,Object> res =new HashMap<>();
////        System.out.println(params);
////        System.out.println(pageSize+"");
////        System.out.println(pageNo);
//        Integer total=usermapper.selectTotal();
//        userName='%'+userName+'%';
//        email='%'+email+'%';
//        address='%'+address+'%';
////        System.out.println(total);
//        List<User>data=usermapper.selectPage(pageNo, pageSize,userName,email,address);
        Result result=new Result<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        IPage<Qwa> page=new Page<>(pageNo,pageSize);
        QueryWrapper<Qwa> workQueryWrapper=new QueryWrapper<>();
//        if(!userName.equals(""))
//            workQueryWrapper.like("user_name",userName);
//        if(!date.equals("")&&date!=null)
//            workQueryWrapper.gt("date", LocalDateTime.parse(date,df));
//        if(!num.equals(""))
//            workQueryWrapper.gt("num",Integer.valueOf(num));
        workQueryWrapper.orderByDesc("id");
        List<Qwa> publications=qwaService.page(page,workQueryWrapper).getRecords();
        page.setTotal(publications.size());
//        res.put("data",data);
//        res.put("total",total);
//        result.setData(res);
//        System.out.println(res);
        System.out.println(qwaService.page(page,workQueryWrapper));
        return qwaService.page(page,workQueryWrapper) ;
    }
    @PostMapping("/add")
    @ApiOperation(value = "添加问答对信息")
    public Result add(@RequestBody Qwa qwa){
        Result result=new Result<>();
        Boolean p=qwaService.save(qwa);
        if(p)
        {
            LambdaQueryWrapper<Qwa> dialectLambdaQueryWrapper=new LambdaQueryWrapper<>();
            dialectLambdaQueryWrapper.eq(Qwa::getCai,qwa.getCai());
            dialectLambdaQueryWrapper.eq(Qwa::getDialectId,qwa.getDialectId());
            dialectLambdaQueryWrapper.eq(Qwa::getAnswer,qwa.getAnswer());
            dialectLambdaQueryWrapper.eq(Qwa::getQuestion,qwa.getQuestion());
            dialectLambdaQueryWrapper.eq(Qwa::getZan,qwa.getZan());
            List<Qwa> dialect1=qwaMapper.selectList(dialectLambdaQueryWrapper);
            if(dialect1.size()!=0)
            {
                result.setResult(dialect1.get(dialect1.size()-1));
                return result;
            }
            else
            {
                result.setResult(false);
                return result;
            }
        }
        else
        {
            result.setResult(false);
            return result;
        }
    }
    //
    //根据主键删除
    @GetMapping("/delete")
    @ApiOperation(value = "删除问答对信息")
    public Result deleteById(@RequestParam String id)
    {
        Result result=new Result<>();
        result.setResult(qwaService.removeById(id));
        return result;
    }
    @PostMapping("/deleteAll")
    @ApiOperation(value = "删除一群问答对信息")
    public Result deleteAll(@RequestBody List<String> ids)
    {
        Result result=new Result<>();
        result.setResult(qwaService.removeByIds(ids));
        return result;
    }
    //
    //根据姓名删除 构造条件即可
//    @GetMapping("/deleteByName")
//    @ApiOperation(value = "根据名字删除工作信息")
//    public Result deleteByName(@RequestParam String username)
//    {
//        Result result=new Result<>();
//        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
//        userQueryWrapper.eq("username",username);
//        User user=userservice.getOne(userQueryWrapper);
//
//        result.setData(userservice.removeById(user.getId()));
//        return result;
//    }
    @PostMapping("/get")
    @ApiOperation("获取问答对信息")
    public Qwa get(@RequestBody Qwa work)
    {
        return qwaService.getById(work.getId());
    }
    //
    @PostMapping("/update")
    @ApiOperation("修改问答对信息")
    public boolean update(@RequestBody Qwa work)
    {
        System.out.println(work);
        QueryWrapper<Qwa>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",work.getId());
        return qwaService.update(work,queryWrapper);
    }

}

